using System.IO;
using QFramework;
using UnityEditor.Hardware;
using UnityEngine.Events;
using File = UnityEngine.Windows.File;

namespace QGameFramework.Tools
{
    public partial class Tool : IUtility
    {
        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="path">文件路径/param>
        /// <param name="info">信息</param>
        /// <param name="Callback">回调函数</param>
        /// <typeparam name="T">泛型</typeparam>
        public void ReadFormFile<T>(string path, out string info, UnityAction<string, T> Callback = null)
            where T : class, new()
        {
            if (File.Exists(path))
                using (StreamReader sr = new StreamReader(path))
                {
                    info = sr.ReadToEnd();
                    T t = info.FromJson<T>();
                    Callback?.Invoke(info, t);
                }
            else
            {
                info = string.Empty;
                T t = new T();
                Callback?.Invoke(info, t);
            }
        }

        public void ReadFormFile()
        {
            
        }

        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="path">写入路径</param>
        /// <param name="temp">泛型数据</param>
        /// <param name="Callback">回调函数</param>
        /// <typeparam name="T">泛型</typeparam>
        public void Write2File<T>(string path, T temp, UnityAction Callback = null) where T : class, new()
        {
            if (!Directory.Exists(path.GetFolderPath()))
            {
                Directory.CreateDirectory(path.GetFolderPath());
            }

            using (StreamWriter sw = new StreamWriter(path))
            {
                sw.Write(temp.ToJson());
                Callback?.Invoke();
            }
        }

        public void Write2File()
        {
            
        }
    }
}